<script setup lang="ts" name="SystemSmsChannel">
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { getIntDictOptions } from '@/utils/dict'
import { DICT_TYPE } from '@/enums/dictEnums'
import { dateTimeFormatter } from '@/utils/dateUtil'
import { SmsChannelApi } from '@/api/system/messages/sms/channel'
import SmsChannelForm from './SmsChannelForm.vue'
import { checkPermit, computedOperateWidth } from '@/utils/permit'
const appStore = useAppStoreWithOut()
const { systemConfig } = storeToRefs(appStore)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const total = ref(0) // 列表的总页数
const list = ref([]) // 表格数据
const queryLoading = ref(false) // 列表加载中：1）列表数据加载；2）查询按钮禁用；3）重置按钮禁用
const queryParams = ref({
  pageIndex: 1,
  pageSize: 20,
  signature: undefined,
  create_time: [],
  status: undefined
})
const queryFormRef = ref() // 搜索的表单


/** 查询列表 */
const getList = async () => {
  queryLoading.value = true
  try {
    const data = await SmsChannelApi.page(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    queryLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageIndex = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.pageIndex = 1
  queryParams.value.pageSize = 20
  queryParams.value.signature = undefined
  queryParams.value.create_time = []
  queryParams.value.status = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SmsChannelApi.delete(id)
    message.success(t('message.del', 'success'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<template>
  <PageCard>
    <ContentFormTable>
      <template #header>
        <ElForm ref="queryFormRef" :model="queryParams" :inline="true">
          <ElFormItem :label="t('sys.messages.sms.channel', 'signature')" prop="signature" class="!w-240px">
            <el-input v-model="queryParams.signature" :placeholder="t('sys.messages.sms.channel.rules', 'signature')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem :label="t('common', 'status')" prop="status" class="!w-240px">
            <el-select v-model="queryParams.status" clearable :placeholder="t('sys.messages.sms.channel.rules', 'status')" @change="handleQuery">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </ElFormItem>
          <ElFormItem :label="t('common', 'createTime')" prop="create_time">
            <el-date-picker
              v-model="queryParams.create_time"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
              clearable
              :start-placeholder="t('common', 'startDate')"
              :end-placeholder="t('common', 'endDate')"
              type="daterange"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleQuery" />
          </ElFormItem>
          <ElFormItem>
            <el-button type="primary" :disabled="queryLoading" icon="Search" @click="handleQuery">{{ t('action',
              'search') }}</el-button>
            <el-button icon="Refresh" :disabled="queryLoading" @click="resetQuery">{{ t('action', 'reset')
            }}</el-button>
            <el-button v-if="checkPermit(['system:sms-channel:create'])" type="success" icon="Plus" @click="openForm('create')">{{ t('action', 'create') }}</el-button>
          </ElFormItem>
        </ElForm>
      </template>
      <template #content>
        <el-table v-loading="queryLoading && systemConfig.loadingType === 'module'" :data="list" border>
          <el-table-column prop="id" :label="t('common', 'id')" width="60" align="center" />
          <el-table-column prop="signature" :label="t('sys.messages.sms.channel', 'signature')" align="center" />
          <el-table-column align="center" :label="t('sys.messages.sms.channel', 'code')" prop="code">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE" :value="scope.row.code" />
            </template>
          </el-table-column>
          <el-table-column prop="status" :label="t('common', 'status')" width="90" align="center">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column :label="t('common', 'remark')" align="center" prop="remark" />
          <el-table-column :label="t('sys.messages.sms.channel', 'apiKey')" align="center" prop="apiKey" :show-overflow-tooltip="true" width="180" />
          <el-table-column :label="t('sys.messages.sms.channel', 'apiSecret')" align="center" prop="apiSecret" :show-overflow-tooltip="true" width="180" />
          <el-table-column :label="t('sys.messages.sms.channel', 'callbackUrl')" align="center" prop="callbackUrl" :show-overflow-tooltip="true" width="180" />
          <el-table-column prop="create_time" :label="t('common', 'createTime')" :formatter="dateTimeFormatter" align="center" />
          <el-table-column v-if="checkPermit(['system:sms-channel:update', 'system:sms-channel:delete'])" prop="operate" :label="t('common', 'operate')" :width="computedOperateWidth(['system:sms-channel:update', 'system:sms-channel:delete'])" align="center" fixed="right">
            <template #default="scope">
              <el-button v-if="checkPermit(['system:sms-channel:update'])" type="primary" icon="Edit" @click="openForm('update', scope.row.id)" />
              <el-button v-if="checkPermit(['system:sms-channel:delete'])" type="danger" icon="Delete" @click="handleDelete(scope.row.id)" />
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageIndex" :total="total" :disabled="queryLoading" @pagination="getList" />
        <!-- 表单弹窗：添加/修改 -->
        <SmsChannelForm ref="formRef" @success="getList" />
      </template>
    </ContentFormTable>
  </PageCard>
</template>

<style scoped lang="scss">
.el-table {
  width: 100%;
  height: 100%;
}
</style>
